﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using S4Store.Domain;
using S4Store.Service;
using S4Store.Models.Chart;
using S4Store.Models.Zh;
using S4Store.Models;

namespace S4Store.Controllers
{
    public class ZhController : ReportBaseController
    {

        public ReportService ReportService { get; set; }
        public SellerKpiService SellerKpiService { get; set; }
        public EmployeeService SellerService { get; set; }
        public CarKpiService CarKpiService { get; set; }
        public CarService CarService { get; set; }


        public ActionResult Index()
        {
            IndexModel model = new IndexModel();
            model.ChartData.Name = "销售顾问新车置换KPI";
            model.ChartData.YAxis = "置换率(%)";
            model.ChartData.Colors = new List<string> { S4Store.Models.Chart.Color.Green, S4Store.Models.Chart.Color.Blue };
            var seriestime = new ChartSeries();
            seriestime.Name = "目标置换率";
            var seriessale = new ChartSeries();
            seriessale.Name = "实际置换率";
            model.ChartData.Serieses.Add(seriestime);
            model.ChartData.Serieses.Add(seriessale);
            using (var t = new Transtion())
            {
                t.Inject(ReportService);
                t.Inject(SellerKpiService);
                //model.IsMonthConfigConfirmed = SellerKpiService.IsMonthConfiged(Month.Year, Month.Month, Store.Id);
                foreach (var seller in Store.GetEnbleEmployee(EmployeeType.FieldSaler | EmployeeType.TelSaler))
                {
                    var report = ReportService.GetSellerPersonReport(Month.Year, Month.Month, Store.Id, seller.Id);
                    seriestime.Data.Add(report.SellerPerformanceStandard.ZHLV);
                    seriessale.Data.Add(report.ZHLV);

                    //差值
                    var value = (report.ZHCJS - report.SellerPerformanceStandard.ZHLV * report.JCS / 100).AutoCeling();
                    if (report.JCS == 0)
                    {
                        model.ChartData.XAxis.Add(seller.Name + "(交车数：0)");
                    }
                    else
                    {
                        model.ChartData.XAxis.Add(seller.Name);
                    }
                    RedGreenIndex index = new RedGreenIndex();
                    index.Size = RedGreenIndex.IndexSize.Middle;
                    if (value >= 0)
                    {
                        index.Type = RedGreenIndex.IndexType.Green;
                    }
                    else
                    {
                        index.Type = RedGreenIndex.IndexType.Red;
                        index.Value = ((int)value).ToString();
                    }
                    index.Tip = seller.Name + " 销售顾问新车置换KPI";
                    index.Title = "实际置换台次相对于置换目标的差值";
                    index.Url = Url.Action("ByPerson", new { SearchSellerId = seller.Id });
                    model.RedGreenIndexs.Add(index);
                }
                t.Commit();
            }
            this.InjectReportModel(model);
            model.BarModel.Action = "Index";
            model.BarModel.Controller = "Zh";
            model.BarModel.AddView(ReportBarModel.ViewItem.Month);
            return View(model);
        }

        public ActionResult ByPerson()
        {
            if (Start == null)
            {
                Start = DateTime.Now.AddMonths(-6);
                End = DateTime.Now;
            }
            ByPersonModel model = new ByPersonModel();
            using (var t = new Transtion())
            {
                t.Inject(ReportService);
                t.Inject(SellerService);
                t.Inject(SellerKpiService);
                t.Inject(CarKpiService);
                t.Inject(CarService);
                model.Seller = SellerService.GetQuery().FirstOrDefault(x => x.Id == SearchSellerId.Value);
                model.IsMonthConfigConfirmed = SellerKpiService.IsMonthConfiged(Month.Year, Month.Month, Store.Id);
                //foreach (var seller in Store.EnbleSellers)
                //{
                //var brands = ReportService.GetCarBrands(Month.Year, Month.Month, Store.Id);
                
                var brands = CarKpiService.GetCarbrandsForStoreMonth(Month.Year, Month.Month, Store.Id).ToList();
                foreach (var brand in brands)
                {
                    var report = ReportService.GetSellerPersonBrandReport(Month.Year, Month.Month, Store.Id, SearchSellerId.Value, brand.Id);
                    report.Seller = model.Seller;
                    report.CarBrand = brand;
                    model.SellerKpiPersonBodyModel.Reports.Add(report);
                }
                model.SellerPersonReport = ReportService.GetSellerPersonReport(Month.Year, Month.Month, Store.Id, SearchSellerId.Value);
                //}
                t.Commit();
            }


            using (var t = new Transtion())
            {
                t.Inject(ReportService);
                t.Inject(SellerService);
                t.Inject(SellerKpiService);
                t.Inject(CarKpiService);

                model.Seller = SellerService.GetQuery().FirstOrDefault(x => x.Id == SearchSellerId.Value);
                for (DateTime dt = Start.Value; dt <= End; dt = dt.AddMonths(1))
                {
                    var report = ReportService.GetSellerPersonReport(dt.Year, dt.Month, Store.Id, SearchSellerId.Value);
                    report.Seller = model.Seller;
                    model.ChartReports.Add(report);
                }
                t.Commit();
            }
            this.InjectReportModel(model);
            return View(model);
        }


        public ActionResult CarBrandIndex()
        {
            IndexModel model = new IndexModel();
            model.ChartData.Name = "车型置换KPI";
            model.ChartData.YAxis = "置换率(%)";
            model.ChartData.Colors = new List<string> { S4Store.Models.Chart.Color.Green, S4Store.Models.Chart.Color.Blue };
            var seriestime = new ChartSeries();
            seriestime.Name = "目标置换率";
            var seriessale = new ChartSeries();
            seriessale.Name = "实际置换率";
            model.ChartData.Serieses.Add(seriestime);
            model.ChartData.Serieses.Add(seriessale);
            using (var t = new Transtion())
            {
                t.Inject(ReportService);
                t.Inject(SellerKpiService);
                t.Inject(CarKpiService);
                //model.IsMonthConfigConfirmed = SellerKpiService.IsMonthConfiged(Month.Year, Month.Month, Store.Id);
                var storereport = ReportService.GetSaleReport(Month.Year, Month.Month, Store.Id);
                seriestime.Data.Add(storereport.CarKpiMonthConfig.ReplacementTarget);
                seriessale.Data.Add(storereport.ZHBL);
                model.ChartData.XAxis.Add("总置换率");
                RedGreenIndex index0 = new RedGreenIndex();
                //差值
                var storereportvalue = (storereport.ZHSL - storereport.CarKpiMonthConfig.ReplacementTarget * storereport.GiveCarCount / 100).AutoCeling();
                if (storereportvalue >= 0)
                {
                    index0.Type = RedGreenIndex.IndexType.Green;
                }
                else
                {
                    index0.Type = RedGreenIndex.IndexType.Red;
                    index0.Value = ((int)storereportvalue).ToString();
                }
                index0.Size = RedGreenIndex.IndexSize.Middle;
                model.RedGreenIndexs.Add(index0);
                var brands = CarKpiService.GetCarbrandsForStoreMonth(Month.Year, Month.Month, Store.Id).ToList();
                foreach (var brand in brands)
                {
                    var report = ReportService.GetCarBrandSaleReport(Month.Year, Month.Month, Store.Id, brand.Id);
                    seriestime.Data.Add(report.CarKpiMonthConfig.ReplacementTarget);
                    seriessale.Data.Add(report.ZHBL);
                    //差值
                    var value = (report.ZHSL - report.CarKpiMonthConfig.ReplacementTarget * report.GiveCarCount / 100).AutoCeling();
                    if (report.GiveCarCount == 0)
                    {
                        model.ChartData.XAxis.Add(brand.Name + "(交车数：0)");
                    }
                    else
                    {
                        model.ChartData.XAxis.Add(brand.Name);
                    }
                    RedGreenIndex index = new RedGreenIndex();
                    index.Size = RedGreenIndex.IndexSize.Middle;
                    if (value >= 0)
                    {
                        index.Type = RedGreenIndex.IndexType.Green;
                    }
                    else
                    {
                        index.Type = RedGreenIndex.IndexType.Red;
                        index.Value = ((int)value).ToString();
                    }
                    index.Tip = brand.Name + " 车款置换KPI";
                    index.Title = "实际置换台次相对于置换目标的差值";
                    index.Url = Url.Action("ByBrand", new { SearchCarBrandId = brand.Id });
                    model.RedGreenIndexs.Add(index);

                }
                t.Commit();
            }
            this.InjectReportModel(model);
            model.BarModel.Action = "CarBrandIndex";
            model.BarModel.Controller = "Zh";
            model.BarModel.AddView(ReportBarModel.ViewItem.Month);
            return View(model);
        }

        public ActionResult ByBrand()
        {
            if (Start == null)
            {
                Start = DateTime.Now.AddMonths(-6);
                End = DateTime.Now;
            }
            ByBrandModel model = new ByBrandModel();
            using (var t = new Transtion())
            {
                t.Inject(ReportService);
                t.Inject(SellerService);
                t.Inject(SellerKpiService);
                t.Inject(CarService);
                t.Inject(CarKpiService);
                model.CarBrand = CarService.GetCarBrandQuery().FirstOrDefault(x => x.Id == SearchCarBrandId.Value);
                //model.IsMonthConfigConfirmed = CarKpiService.IsMonthConfiged(Month.Year, Month.Month, Store.Id);
                //foreach (var seller in Store.EnbleSellers)
                //{
                var carmodels = CarKpiService.GetCarmodelsForStoreMonth(Month.Year, Month.Month, Store.Id, SearchCarBrandId.Value).ToList();
                foreach (var carmodel in carmodels)
                {
                    var report = ReportService.GetCarModelSaleReport(Month.Year, Month.Month, Store.Id, carmodel.Id);
                    //report.Seller = model.Seller;
                    //report.CarBrand = brand;
                    model.CarKpiPersonBodyModel.Reports.Add(report);
                }
                model.BrandReport = ReportService.GetCarBrandSaleReport(Month.Year, Month.Month, Store.Id, SearchCarBrandId.Value);

                var currentmonthReport = ReportService.GetCarBrandSaleReport(Month.Year, Month.Month, Store.Id, SearchCarBrandId.Value);
                model.ZHMB = currentmonthReport.CarKpiMonthConfig.ReplacementTarget;
                model.ZHCY = currentmonthReport.ZHDiff;
                t.Commit();
            }


            using (var t = new Transtion())
            {
                t.Inject(ReportService);
                t.Inject(SellerService);
                t.Inject(SellerKpiService);
                t.Inject(CarKpiService);

                for (DateTime dt = Start.Value; dt <= End; dt = dt.AddMonths(1))
                {
                    var report = ReportService.GetCarBrandSaleReport(dt.Year, dt.Month, Store.Id, SearchCarBrandId.Value);
                    //report.Seller = model.Seller;
                    model.ChartReports.Add(report);
                }
                t.Commit();
            }
            this.InjectReportModel(model);
            return View(model);
        }
    }
}
